package net.tngou.iask.action.my;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Address;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Main;
import net.tngou.iask.pojo.Main_disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.pojo.Type;
import net.tngou.iask.pojo.User;
import net.tngou.iask.service.DiseaseService;
import net.tngou.iask.service.MainService;
import net.tngou.iask.service.SurveyService;

/**
 * 实体
 * @author tngou@tngou.net
 *
 */
public class MainAction extends BaseAction {
	@Override
	public void execute() throws ServletException, IOException {
		if(user.getRole()!=3)
		{
			run_500();return;
		}
		Main main = new Main().get(user.getMain());
		if(main==null){
			run_404();return;
		}
		Type type = new Type().get(main.getType());
		if(type==null)type=new Type();
		type.setName("私有云");
		type.setDescription("私有云部署版");
		POJO address = new Address().get(main.getProvince());
		root.put("prov", address);
		root.put("maintype", type);
		printFreemarker("my/main.ftl", root);
	}
	
	/**
	 * 实体列表
	 */
	@SuppressWarnings("serial")
	public void list() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		long id=ask.getId();
		MainService service = new MainService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), id, ask.getKeyword());
		root.put("page", page);
		root.put("province", id);
		List<?> list =service.getList("type=2", Address.class);
		root.put("list", list);
		List<?> types = service.getList(Type.class);
		root.put("types", types);
		printFreemarker("my/main_list.ftl", root);
	}
	/**
	 * 添加实体
	 */
	public void add() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		long id=ask.getId();
		if(id!=0){
			POJO prov = new Address().get(id);
			root.put("prov", prov);
		}
		List<?> list = service.getList(Type.class);
		root.put("list", list);
		printFreemarker("my/main_add.ftl", root);
	}
	
	/**
	 * 更新实体
	 */
	public void update() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		long id=ask.getId();
		if(id==0){run_404();return;}
		Main main = new Main().get(id);
		if(main==null)
		{
			run_404();return;
		}
		root.put("m", main);
		POJO address = new Address().get(main.getProvince());
		root.put("prov", address);
		List<?> list = service.getList(Type.class);
		root.put("list", list);
		printFreemarker("my/main_update.ftl", root);
	}
	
	/**
	 * 用户列表
	 */
	public void userlist() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		long id=ask.getId();
		if(id==0){run_404();return;}
		Main main = new Main().get(id);
		if(main==null){run_404();return;}
		List<?> list = service.getList("main="+main.getId()+" AND role=3", User.class);
		root.put("m", main);
		root.put("list", list);
		printFreemarker("my/main_userlist.ftl", root);
		
	}
	
	/**
	 * 添加用户
	 */
	public void useradd() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		long id=ask.getId();
		if(id==0){run_404();return;}
		Main main = new Main().get(id);
		if(main==null){run_404();return;}
		root.put("m", main);		
		printFreemarker("my/main_useradd.ftl", root);
		
	}
	
	/**
	 * 用户更新
	 */
	public void userupdate() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		int mainid=0;
		int id=0;
		String[] params = request.getParams();
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			mainid=Integer.parseInt(params[0]);	
			if(params.length>1&&NumberUtils.isNumber(params[1]))
				id=Integer.parseInt(params[1]);
		}
		if(id==0||mainid==0){run_404();return;}
		User u = new User().get(id);
		Main main = new Main().get(mainid);
		if(u==null||main==null||u.getMain()!=main.getId()){run_404();return;}
		root.put("u", u);
		root.put("m", main);
		printFreemarker("my/main_userupdate.ftl", root);
	}
	
	/**
	 * 重置密码
	 */
	public void userinit() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		int mainid=0;
		int id=0;
		String[] params = request.getParams();
		if(params!=null&&params.length>0&&NumberUtils.isNumber(params[0]))
		{	
			mainid=Integer.parseInt(params[0]);	
			if(params.length>1&&NumberUtils.isNumber(params[1]))
				id=Integer.parseInt(params[1]);
		}
		if(id==0||mainid==0){run_404();return;}
		User u = new User().get(id);
		Main main = new Main().get(mainid);
		if(u==null||main==null||u.getMain()!=main.getId()){run_404();return;}
		root.put("u", u);
		root.put("m", main);
		printFreemarker("my/main_userinit.ftl", root);
	}
	
	/**
	 * 疾病列表
	 */
	public void diseaselist() {
		if(user.getRole()!=3&&user.getRole()!=4)
		{
			run_500();return;
		}
		DiseaseService service = new DiseaseService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		printFreemarker("my/main_diseaselist.ftl", root);
	}
	
	/**
	 * 疾病添加
	 */
	public void diseaseadd() {
		if(user.getRole()!=3&&user.getRole()!=4)
		{
			run_500();return;
		}
		
		DiseaseService service = new DiseaseService();
		PageUtil page = service.getNotDisease(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		printFreemarker("my/main_diseaseadd.ftl", root);
	}
	
	/**
	 * 更新疾病
	 */
	public void diseaseupdate() {
		if(user.getRole()!=3&&user.getRole()!=4)
		{
			run_500();return;
		}
		long id = ask.getId();
		if(id==0){run_404();return;}
		Main_disease main_disease = new Main_disease().get(id);
		if(main_disease==null){run_404();return;}
		Disease disease = new Disease().get(main_disease.getDisease());
//		List<Survey> list = (List<Survey>) service.getList("main="+user.getMain()+" AND type=2", Survey.class);
//		SurveyService service = new SurveyService();
//		List<Survey> lis = service.getSurvey(main_disease.getDisease());
//		list.addAll(lis);
		SurveyService service = new SurveyService();
		List<Survey> list = service.getSurvey(disease.getId());
		@SuppressWarnings("unchecked")
		List<Survey> ls = (List<Survey>) service.getList("main="+user.getMain()+" AND type=2", Survey.class);
		if(list!=null)
			list.addAll(ls);
		else
			list=ls;
		Survey survey = new Survey().get(2);
		if(list==null){
			list = new ArrayList<>();
			list.add(survey);
		}
		root.put("list", list);
		root.put("disease", disease);
		root.put("main_disease", main_disease);
		printFreemarker("my/main_diseaseupdate.ftl", root);
		
		
	}
}
